<template>
  <div class="brand">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button v-btn-permi:add type="primary" size="default" @click="add">新增</el-button>
      </div>
      <div class="table-holder">
        <!-- 品牌展示表格 -->
        <el-table :data="brandList" border style="width: 100%">
          <el-table-column fixed="left" type="index" label="#" width="50" />
          <el-table-column prop="name" label="品牌名称" width="200" />
          <el-table-column label="展示状态" width="200">
            <template slot-scope="scope">
              <span>是否展示：</span>
              <el-switch
                v-model="scope.row.showStatus"
                :active-value="1"
                :inactive-value="0"
                @change="switchShow($event, scope.row.id)"
              />
            </template>
          </el-table-column>
          <el-table-column label="logo" width="200">
            <template slot-scope="scope">
              <img class="avatar" :src="scope.row.logo" alt="">
            </template>
          </el-table-column>
          <el-table-column label="专区大图" width="200">
            <template slot-scope="scope">
              <img class="avatar" :src="scope.row.bigPic" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="brandStory" label="品牌故事" width="200" />
          <el-table-column prop="createTime" label="创建时间" width="200" />
          <el-table-column prop="sort" label="排序" width="200" />
          <el-table-column
            prop="productCommentCount"
            label="产品评论数量"
            width="200"
          />
          <el-table-column prop="productCount" label="产品数量" width="200" />
          <el-table-column label="是否产品制造商" width="200">
            <template slot-scope="scope">
              <span v-if="scope.row.factoryStatus === 0">是</span>
              <span v-if="scope.row.factoryStatus === 1">不是</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                v-btn-permi:edit
                type="text"
                size="small"
                @click="edit(scope.row)"
              >编辑</el-button>
              <el-button
                v-btn-permi:remove
                type="text"
                style="color: red"
                size="small"
                @click="del(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination-holder">
        <el-pagination
          :current-page="page.start"
          :page-sizes="page.pageSize"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog title="编辑品牌" :visible.sync="dialogFormVisible">
      <el-form :model="brandInfo">
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="品牌名称">
              <el-input v-model="brandInfo.name" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="首字母">
              <el-input v-model="brandInfo.firstLetter" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="排序">
              <el-input v-model.number="brandInfo.sort" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="产品数量">
              <el-input v-model.number="brandInfo.productCount" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="是否展示">
              <el-switch
                v-model="brandInfo.showStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="是否为品牌制造商">
              <el-switch
                v-model="brandInfo.factoryStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="上传专区大图">
              <div class="upload-holder">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="uploadHeaders"
                  :on-success="uploadBigImgSuccess"
                  :limit="1"
                  :show-file-list="false"
                >
                  <img
                    v-if="brandInfo.bigPic"
                    :src="brandInfo.bigPic"
                    class="avatar"
                  >
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="上传logo">
              <div class="upload-holder">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="uploadHeaders"
                  :on-success="uploadLogoSuccess"
                  :limit="1"
                  :show-file-list="false"
                >
                  <img
                    v-if="brandInfo.logo"
                    :src="brandInfo.logo"
                    class="avatar"
                  >
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          @click="submit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  requestBrandListByPage,
  deleteBrand,
  switchShowStatus,
  addBrand,
  updateBrand
} from '@/api/product'
import mixin from '@/mixins/index'
export default {
  mixins: [mixin],
  data() {
    return {
      brandList: [],
      dialogFormVisible: false,
      brandInfo: {},
      isAdd: false
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        this.brandInfo = {}
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 关闭弹窗
    close() {
      this.dialogFormVisible = false
    },
    // 提交操作
    addBrandMethod() {
      addBrand(this.brandInfo).then(res => {
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message({
            type: 'error',
            message: '添加失败!'
          })
        }
      })
    },
    // 编辑操作
    updateBrandMethod() {
      updateBrand(this.brandInfo).then(res => {
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message({
            type: 'error',
            message: '修改失败!'
          })
        }
      })
    },
    // 确定提交
    submit() {
      if (this.isAdd) {
        this.addBrandMethod()
      } else {
        this.updateBrandMethod()
      }
    },
    // 上传图片成功的钩子
    uploadBigImgSuccess(res) {
      // console.log(res)
      if (res.code === 20000) {
        this.$set(this.brandInfo, 'bigPic', res.data.fileUrl)
      }
    },
    // 上传logo成功的钩子
    uploadLogoSuccess(res) {
      // console.log(res)
      if (res.code === 20000) {
        this.$set(this.brandInfo, 'logo', res.data.fileUrl)
      }
    },
    // 切换是否显示
    switchShow(e, id) {
      // console.log(e, id)
      switchShowStatus({ id, status: e }).then((res) => {
        // console.log(res)
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '切换状态成功!'
          })
        } else {
          this.$message({
            type: 'error',
            message: '切换状态失败!'
          })
        }
        this.getData()
      })
    },
    // 新增品牌
    add() {
      this.dialogFormVisible = true
      this.isAdd = true
    },
    // 编辑品牌
    edit(item) {
      this.dialogFormVisible = true
      this.isAdd = false
      this.brandInfo = { ...item }
    },
    // 删除品牌
    del(id) {
      this.$confirm('此操作将永久删除该品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteBrand(id).then((res) => {
            // console.log(res)
            if (res.code === 20000) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getData()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取品牌数据
    getData() {
      requestBrandListByPage(this.page).then((res) => {
        // console.log(res)
        this.brandList = res.data.rows
        this.page.total = res.data.total
      })
    },
    // 改变每页数量
    handleSizeChange(val) {
      this.page.limit = val
      this.getData()
    },
    // 改变页码
    handleCurrentChange(val) {
      this.page.start = val
      this.getData()
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style lang="scss" scoped>
.brand {
  .avatar {
    width: 100px;
    height: 100px;
  }
}
</style>
